/*
*┌─────────────────────────────────┐
*│　描   述：
*│　类   名：InitializeLeaderBroad
*│　创  建  人：DD
*│　Copyright(C) 2020 by DD
*└─────────────────────────────────┘
*┌─────────────────────────────────┐
*│　修  改  人：
*│　修改方向：
*└─────────────────────────────────┘
*/
using System;

using GooglePlayGames;
using GooglePlayGames.BasicApi;

using UnityEngine;

namespace DD
{
    public class InitializeLeaderBroad : SingleBase<InitializeLeaderBroad>
    {

        public const string leaderboard_best_score="CgkIxvKm3JAMEAIQAA";

        protected static bool isLogged;

        public void OnSignIn( )
        {
            // 查看Google服务输出信息 recommended for debugging:

            PlayGamesPlatform.DebugLogEnabled = true;


            if ( isLogged )
            {
                return;
            }
                //激活谷歌服务 
                PlayGamesPlatform.Activate ();

                Debug.LogError ( "google play激活成功" );
                //判断用户是否登录
                SignIn ();
                Debug.LogError ( "google play servers sign in" );
        }

        void SignIn( )
        {
            // 认证用户，判定用户是否登录:
            Social.localUser.Authenticate ( (bool success) =>
            {
                if ( success )
                {
                    isLogged = true;
                }
                else
                {
                    isLogged = false;
                }
            } );
        }

        //取本地的HighScore与对应排行榜线上的分数作比较时，使用此方法，googlePlayLeaderboardID为排行榜ID
        public void ReportScore(long HighScore , string googlePlayLeaderboardID)
        {
            Debug.Log ( "开始上传分数" );
            if ( !isLogged )
            {
                Debug.LogError ( "isLogged or not" );
                return;
            }

            PlayGamesPlatform.Instance.LoadScores (

                googlePlayLeaderboardID ,
                LeaderboardStart.PlayerCentered ,
                1 ,
                LeaderboardCollection.Public ,
                LeaderboardTimeSpan.AllTime ,
                (data) =>
                {
                    //Debug.Log ( "加载到服务器分数并开始比较" );
                    if ( data.Valid )
                    {
                        Debug.Log ( "加载到服务器分数并开始比较" );
                        long score = 0;

                        //Debug.Log ( "加载到服务器分数并开始比较" );
                        try
                        {
                            score = data.PlayerScore.value;
                        }
                        catch ( Exception exc )
                        {
                            Debug.Log ( exc );
                        }
                        finally
                        {
                            if ( score < HighScore )
                            {
                                Debug.LogError ( "本地分數大于服务器分数，开始上传" );
                                Social.ReportScore ( HighScore , googlePlayLeaderboardID , (bool success) =>
                                {
                                    Debug.LogError ( "上传分数成功" );
                                } );
                            }
                            else
                            {
                                Debug.LogError ( "本地分數小于服务器分数，不用上传" );
                            }
                        }

                        //Debug.LogError ( "score from inside leaderboard: " + data.PlayerScore.value );
                        //Debug.LogError ( "formated score from inside leaderboard: " + data.PlayerScore.formattedValue );

                    }
                    else
                    {
                        Debug.LogError ( "上传失败" );
                    }
                } );
        }
        //在本地已经做了排序处理，只提交自己需要提交的相关分数时 使用此方法，leaderboardIdToUse为对应排行榜ID
        public void SendHighScore(long score , string leaderboardIdToUse)
        {
            try
            {
                Social.ReportScore ( score , leaderboardIdToUse , (bool success) =>
                {

                    Debug.LogError ( "上传分数成功" );
                } );
            }
            catch
            {
                Debug.LogError ( "上传失败" );
            }
        }

        //显示排行榜
        public void showLeaderboard( )
        {
            //已经登陆
            if ( isLogged )
            {

                //显示对应ID排行榜
                PlayGamesPlatform.Instance.ShowLeaderboardUI ( leaderboard_best_score );
                //显示所有排行榜
                //Social.ShowLeaderboardUI ();
            }
            else
            {
                //提示
                //Tips.OnShowTIpsHandler ( "Play Rank Battle First" );
                //Tips.OnTriggerTips ( "Play Rank Battle First" );
            }
        }
    }
}